{% macro handlerComment(name, exceptionNumber) -%}
{{ name }}, address {{ '{:#x}'.format(exceptionNumber * 4) }}, exception number {{ exceptionNumber }}, IRQ number {{ exceptionNumber - 16 }}
{%- endmacro %}
/**
 * \file
 * \brief {{ board }} ({{ dictionary['chip']['compatible'][0] }} chip) vector table, default handlers and NVIC low-level initializer
 *
 * \author Copyright (C) 2014-2019 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
 *
 * \par License
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
 * distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \warning
 * Automatically generated file - do not edit!
 */

#include "distortos/chip/CMSIS-proxy.h"

#include "distortos/BIND_LOW_LEVEL_INITIALIZER.h"

namespace
{

/*---------------------------------------------------------------------------------------------------------------------+
| local types
+---------------------------------------------------------------------------------------------------------------------*/

/// single exception vector - pointer to function with no arguments and no return value
using ExceptionVector = void(*)();

/*---------------------------------------------------------------------------------------------------------------------+
| local objects
+---------------------------------------------------------------------------------------------------------------------*/

/// priority of interrupts
{% if 'ARM,ARMv7-M' in dictionary['CPUs'][0]['compatible'] %}
#if defined(DISTORTOS_ARCHITECTURE_KERNEL_BASEPRI)
constexpr uint8_t interruptPriority {DISTORTOS_ARCHITECTURE_KERNEL_BASEPRI};
#else	// !defined(DISTORTOS_ARCHITECTURE_KERNEL_BASEPRI)
{% endif %}
constexpr uint8_t interruptPriority {};
{% if 'ARM,ARMv7-M' in dictionary['CPUs'][0]['compatible'] %}
#endif	// !defined(DISTORTOS_ARCHITECTURE_KERNEL_BASEPRI)
{% endif %}

/*---------------------------------------------------------------------------------------------------------------------+
| local functions
+---------------------------------------------------------------------------------------------------------------------*/

/**
 * \brief Low-level peripheral initializer for NVIC
 *
 * This function is called before constructors for global and static objects via BIND_LOW_LEVEL_INITIALIZER().
 */

void nvicLowLevelInitializer()
{
{% for item in dictionary['NVIC']['chip-vectors'] if item %}
#ifdef DISTORTOS_ARCHITECTURE_NVIC_{{ item['name'] | upper }}_ENABLE
	NVIC_SetPriority({{ item['name'] }}_IRQn, interruptPriority);
	NVIC_EnableIRQ({{ item['name'] }}_IRQn);
#endif	// def DISTORTOS_ARCHITECTURE_NVIC_{{ item['name'] | upper }}_ENABLE
{% endfor %}
}

BIND_LOW_LEVEL_INITIALIZER(50, nvicLowLevelInitializer);

}	// namespace

extern "C"
{

/*---------------------------------------------------------------------------------------------------------------------+
| default handlers
+---------------------------------------------------------------------------------------------------------------------*/
{% for exceptionNumber, item in enumerate(dictionary['NVIC']['core-vectors'][1:], 2) if item %}

// {{ handlerComment(item['name'], exceptionNumber) }}
__attribute__ ((weak)) void {{ item['name'] }}_Handler()
{
	while (1);
}
{% endfor %}
{% for exceptionNumber, item in enumerate(dictionary['NVIC']['chip-vectors'],
		len(dictionary['NVIC']['core-vectors']) + 1) if item %}

// {{ handlerComment(item['name'], exceptionNumber) }}
{% if 'subvectors' not in item %}__attribute__ ((weak)) {% endif %}void {{ item['name'] }}_IRQHandler()
{
{% for subvector in item.get('subvectors', []) %}
	__attribute__ ((weak)) void {{ subvector }}_IRQHandler();
{% if loop.last == True %}

{% endif %}
{% endfor %}
{% for subvector in item.get('subvectors', []) %}
	{{ subvector }}_IRQHandler();
{% else %}
	while (1);
{% endfor %}
}
{% endfor %}

/*---------------------------------------------------------------------------------------------------------------------+
| global symbols' declarations
+---------------------------------------------------------------------------------------------------------------------*/

extern const char __main_stack_end[];		// main stack end - imported from linker script

void Reset_Handler();		// import the address of Reset_Handler()

/*---------------------------------------------------------------------------------------------------------------------+
| global objects
+---------------------------------------------------------------------------------------------------------------------*/

/// vector table
const ExceptionVector vectorTable[] __attribute__ ((section(".vectorTable"), used))
{
		reinterpret_cast<ExceptionVector>(__main_stack_end),	// 0x0: Main stack end address
{% for exceptionNumber, item in enumerate(dictionary['NVIC']['core-vectors'], 1) %}
{% if item %}
		{{ item['name'] }}_Handler,		// {{ handlerComment(item['name'], exceptionNumber) }}
{% else %}
		nullptr,		// {{ handlerComment('Reserved', exceptionNumber) }}
{% endif %}
{% endfor %}
{% for exceptionNumber, item in enumerate(dictionary['NVIC']['chip-vectors'],
		len(dictionary['NVIC']['core-vectors']) + 1) %}
{% if item %}
		{{ item['name'] }}_IRQHandler,		// {{ handlerComment(item['name'], exceptionNumber) }}
{% else %}
		nullptr,		// {{ handlerComment('Reserved', exceptionNumber) }}
{% endif %}
{% endfor %}
};

}	// extern "C"
